package com.epam.debrecen.hackathon.ai.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Cards {

	private final Map<Card, Integer> playedOutCards;
	/** user -> cards */
	private final Map<String, List<Card>> faceUpCards;
	private boolean winter = false;
	private boolean spring = false;

	public Cards() {
		this.playedOutCards = new HashMap<Card, Integer>();
		this.faceUpCards = new HashMap<String, List<Card>>();
	}

	/**
	 * Old cards (played out in previous battles)
	 * 
	 * @return
	 */
	public Map<Card, Integer> getPlayedOutCards() {
		return this.playedOutCards;
	}

	public void addPlayedOutCard(Card card) {
		if (this.playedOutCards.get(card) == null) {
			this.playedOutCards.put(card, 0);
		}
		this.playedOutCards.put(card, this.playedOutCards.get(card) + 1);
	}

	public List<Card> getFacedUpCards(String player) {
		return this.faceUpCards.get(player);
	}

	/**
	 * Cards played out in the current battle
	 * 
	 * @param currentPlayer
	 * @param currentCard
	 */
	public void addFaceUpCard(String currentPlayer, Card currentCard) {
		if (this.faceUpCards.get(currentPlayer) == null) {
			this.faceUpCards.put(currentPlayer, new ArrayList<Card>());
		}
		this.faceUpCards.get(currentPlayer).add(currentCard);
		if (currentCard == Card.WINTER) {
			this.spring = false;
			this.winter = true;
		} else if (currentCard == Card.SPRING) {
			this.winter = false;
			this.spring = true;
		}
	}

	/**
	 * Transfer the faceUp cards in to played out cards.
	 */
	public void flushFaceUpCards() {
		Iterator<String> iterator = this.faceUpCards.keySet().iterator();
		while (iterator.hasNext()) {
			String currentKey = iterator.next();
			while (!this.faceUpCards.get(currentKey).isEmpty()) {
				this.addPlayedOutCard(this.faceUpCards.get(currentKey).get(0));
				this.faceUpCards.get(currentKey).remove(0);
			}
		}
		this.spring = false;
		this.winter = false;
	}

	public boolean isSpring() {
		return this.spring;
	}

	public boolean isWinter() {
		return this.winter;
	}

	public Map<String, List<Card>> getFaceUpCards() {
		return this.faceUpCards;
	}

	public int getCurrentPlayerLineValue(Players players) {
		int myLine = 0;
		List<Card> list = this.faceUpCards.get(players.getCurrentPlayer());

		if (list == null) {
			return myLine;
		}

		for (Card c : list) {
			if (!this.winter) {
				myLine += c.getValue();
			} else {
				if (c.isMercenary()) {
					myLine++;
				} else {
					myLine += c.getValue();
				}
			}
		}
		return myLine;
	}

	public int countBiggestLineValue(Players players) {
		int biggestLine = 0;
		for (String player : players.getPlayerSet()) {
			if (player.equals(players.getCurrentPlayer())) {
				continue;
			}
			int currentLine = 0;
			for (Card c : this.faceUpCards.get(player)) {
				if (!this.winter) {
					currentLine += c.getValue();
				} else {
					if (c.isMercenary()) {
						currentLine++;
					} else {
						currentLine += c.getValue();
					}
				}
			}
			if (currentLine > biggestLine) {
				biggestLine = currentLine;
			}
		}
		return biggestLine;
	}

	public void removeTopFaceUpCards() {
		Card max = null;
		for (List<Card> cards : this.getFaceUpCards().values()) {
			for (Card card : cards) {
				if (card.isMercenary() && (max == null || card.getValue() > max.getValue())) {
					max = card;
				}
			}
		}

		if (max != null) {


			System.out.println("MAX:" + max);


			for (Entry<String, List<Card>> card : this.getFaceUpCards().entrySet()) {
				Iterator<Card> i = card.getValue().iterator();

				while (i.hasNext()) {
					if (i.next().equals(max)) {
						i.remove();
					}
				}
			}
		}
	}


	public boolean hasHighValueMerc(Players players){
		for(Card c : faceUpCards.get(players.getCurrentPlayer())){
			if(c.getValue()>4){
				return true;
			}
		}
		return false;
	}
}
